package org.ruiyichen.guizhou.agr.ui.header;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.aspsine.swipetoloadlayout.SwipeRefreshHeaderLayout;

import org.ruiyichen.guizhou.agr.R;


/**
 * Created by wutian
 */
public class RefreshHeaderView extends SwipeRefreshHeaderLayout {

    private TextView refreshDescribe;

    private ImageView progressBar;

    private int mHeaderHeight;

    private boolean rotated = false;

    /**
     * 帧动画 水滴
     */
    private AnimationDrawable animationDrawable;

    public RefreshHeaderView(Context context) {
        this(context, null);
    }

    public RefreshHeaderView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RefreshHeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mHeaderHeight = getResources().getDimensionPixelOffset(R.dimen.refresh_header_height);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        refreshDescribe = (TextView) findViewById(R.id.tv_refresh_header_describe);
        progressBar = (ImageView) findViewById(R.id.iv_refresh_header_progressbar);
        progressBar.setImageResource(R.drawable.image_load_anim);
        animationDrawable = (AnimationDrawable) progressBar.getDrawable();
    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        animationDrawable.start();
    }

    @Override
    public void onRefresh() {
//        progressBar.clearAnimation();
        progressBar.setVisibility(VISIBLE);
//        animationDrawable.start();
        refreshDescribe.setText("正在刷新...");
    }

    @Override
    public void onPrepare() {
        Log.d("PlumRefreshHeader", "onPrepare()");
    }

    @Override
    public void onMove(int y, boolean isComplete, boolean automatic) {
        if (!isComplete) {
//            progressBar.setVisibility(GONE);
            if (y > mHeaderHeight) {
                refreshDescribe.setText("释放刷新~");
                if (!rotated) {
                    rotated = true;
                }
            } else if (y < mHeaderHeight) {
                if (rotated) {
                    rotated = false;
                }
                refreshDescribe.setText("下拉刷新~");
            }

            animationDrawable.start();
        }
    }

    @Override
    public void onRelease() {
        Log.d("TwitterRefreshHeader", "onRelease()");
    }

    @Override
    public void onComplete() {
        rotated = false;
//        progressBar.setVisibility(GONE);
        animationDrawable.stop();
        refreshDescribe.setText("刷新完成~");
    }

    @Override
    public void onReset() {
        rotated = false;
//        progressBar.setVisibility(GONE);
        animationDrawable.stop();
    }

}
